
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.util.ArrayList;

import javax.lang.model.element.Element;
import javax.swing.text.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Nhacso {
    public static AccentRemover artest = new AccentRemover();
    
	public static void main(String[] arguments) throws Exception {
		// BufferedReader in = new BufferedReader(new
		// InputStreamReader(url.openStream(), "UTF8"));
		String inputLine;
		String s = "";
		String[] songURL;
		
		ArrayList<SongVO> arrayListSong = new ArrayList<SongVO>();
		arrayListSong.addAll(getSongs());
		
		for(int j = 0; j < arrayListSong.size(); j++){
            System.out.println("Id : " + arrayListSong.get(j).getId());
            System.out.println("Name : " + arrayListSong.get(j).getName());
            System.out.println("Lyric : " + arrayListSong.get(j).getLyric());    
        }

		
        int LENGTH = getSongs().size();
                
		int songSTT = 0;

		String prefixSongURL = "http://nhacso.net";
		String prefixURL = "http://nhacso.net/tim-kiem/";
		String suffixURL = ".html";

		String[] songLyric = new String[LENGTH];
		
		FileOutputStream fileOutStream;
		fileOutStream = new FileOutputStream("output.txt");

		BufferedWriter bufferedWriter = new BufferedWriter(
				new OutputStreamWriter(fileOutStream, "Unicode"));

		for (int i = 0; i < LENGTH; i++) {
			s = "";
			// Read html page
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new URL(prefixURL + arrayListSong.get(i).getName() + suffixURL).openStream(), "UTF8"));
			
			// Set html page source to String s
			while ((inputLine = in.readLine()) != null) {
				s += inputLine;
			}

			org.jsoup.nodes.Document doc = Jsoup.parse(s);

			// Get all link search song and save to songURL array
			Elements options4 = doc.select("a.name_music_2");
			songURL = new String[options4.size()];
			for (int j = 0; j < options4.size(); j++) {
				org.jsoup.nodes.Element options3 = doc.select("a.name_music_2").get(j);
				String attr3 = options3.attr("href");
				songURL[j] = attr3;
			}

			// Set song lyric default
			songLyric[i] = " ";
						
			// Save lyric to songLyric array
			for (int k = 0; k < songURL.length; k++) {
				s = "";
				BufferedReader in2 = new BufferedReader(new InputStreamReader(
						new URL(songURL[k]).openStream(), "UTF8"));
				while ((inputLine = in2.readLine()) != null) {
					s += inputLine;
				}
				org.jsoup.nodes.Document doc2 = Jsoup.parse(s);
				Elements options = doc2.select("p.desc");
				String attr = options.text();
				System.out.println(attr);
//				System.out.println(attr);   && artest.toUrlFriendly2(attr).contains(getSongs().get(i).getLyric())
				if (!attr.equals("") && artest.toRemoveVNese(attr).contains(artest.toRemoveVNese(getSongs().get(i).getLyric()))) {
					songLyric[i] = attr.substring(10);
					break;
				} 
			}
		}

                int dem = 0;
		for (int i = 0; i < LENGTH; i++) {
//			System.out.println(songLyric[i] + "\n");
                        if(songLyric[i].equals("")) {
                            dem++;
                        }
			bufferedWriter.write(songLyric[i]);
			bufferedWriter.newLine();
			
		}
//                System.out.println(LENGTH);
//                System.out.println(dem);
		bufferedWriter.close();
	}

        public static ArrayList<SongVO> getSongs() {
            
            ArrayList<SongVO> tempSongArray = new ArrayList<SongVO>();
            SongVO song;
            String tempName;
            String tempId;
            String tempLyric;
            
            try {
 
                String fileName = "file.xml";
                File fXMLFile = new File(fileName);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                org.w3c.dom.Document doc = dBuilder.parse(fXMLFile);
                doc.getDocumentElement().normalize();

//                System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

                NodeList nList = doc.getElementsByTagName("song");

//                System.out.println("----------------------------");

                for (int temp = 0; temp < nList.getLength(); temp++) {

                        Node nNode = nList.item(temp);

//                        System.out.println("\nCurrent Element :" + nNode.getNodeName());

                        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                                org.w3c.dom.Element eElement = (org.w3c.dom.Element) nNode;
                                
                                tempId = eElement.getElementsByTagName("id").item(0).getTextContent();
                                tempName = eElement.getElementsByTagName("name").item(0).getTextContent();                                
                                tempLyric = eElement.getElementsByTagName("lyric").item(0).getTextContent();
                                
                                song = new SongVO(tempId, 
                                        artest.toUrlFriendly(tempName.substring(0, tempName.length()-1)), 
                                        tempLyric);
                                
                                tempSongArray.add(song);
                                
                                tempName = "";
                                tempId = "";
                                tempLyric = "";
                                
//                                System.out.println("Name : " + eElement.getElementsByTagName("name").item(0).getTextContent());
//                                System.out.println("Author : " + eElement.getElementsByTagName("author").item(0).getTextContent());
//                                System.out.println("Id : " + eElement.getElementsByTagName("id").item(0).getTextContent());
//                                System.out.println("Lyric : " + eElement.getElementsByTagName("lyric").item(0).getTextContent());

                        }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            
            
//            System.out.println(tempSongArray.size());
            return tempSongArray;
        }

}
